package com.wantong.jianpu.activitys;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.os.Bundle;
import android.widget.ImageView;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.util.SmartGlideImageLoader;
import com.wantong.jianpu.BR;
import com.wantong.jianpu.R;
import com.wantong.jianpu.base.BaseXActivity;
import com.wantong.jianpu.beans.FaultDetailBean;
import com.wantong.jianpu.customviews.FaultAssignView;
import com.wantong.jianpu.customviews.FaultCommitView;
import com.wantong.jianpu.customviews.FaultFeedBackView;
import com.wantong.jianpu.customviews.FaultNotSuccessView;
import com.wantong.jianpu.customviews.FaultReAssignView;
import com.wantong.jianpu.customviews.FaultRepairBackView;
import com.wantong.jianpu.customviews.FaultRepairGoneView;
import com.wantong.jianpu.customviews.FaultRepairingView;
import com.wantong.jianpu.customviews.FaultSuccessView;
import com.wantong.jianpu.databinding.ActivityFaultDetailBinding;
import com.wantong.jianpu.dialogs.ReBackPopupView;
import com.wantong.jianpu.event.FaultDetailEvent;
import com.wantong.jianpu.event.FaultListEvent;
import com.wantong.jianpu.viewmodels.FaultDetailViewModel;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;

/**
 * @author PACY
 * @date 2022/3/23
 * description: 故障详情
 */
public class FaultDetailActivity extends BaseXActivity<ActivityFaultDetailBinding, FaultDetailViewModel> {

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_fault_detail;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();
        setToolbar(binding.baseToolBar);
        EventBus.getDefault().register(this);
        viewModel.id=getIntent().getExtras().getString("id");
        viewModel.reBackDialogEvent.observe(this, aBoolean -> {
            showReBackPopop();
        });

        //配置详情UI
        viewModel.records.observe(this, recordsListDTOS -> {
            if(recordsListDTOS!=null){
                binding.recordsLin.removeAllViews();
                for (FaultDetailBean.RecordsListDTO recordsListDTO : recordsListDTOS) {
                    switch (recordsListDTO.getStatus()){
                        case 1://工单退回
                            FaultFeedBackView feedBackView = new FaultFeedBackView(FaultDetailActivity.this);
                            feedBackView.setData(recordsListDTO);
                            binding.recordsLin.addView(feedBackView);
                            break;
                        case 2://工单提交
                            FaultCommitView commitView = new FaultCommitView(FaultDetailActivity.this);
                            commitView.setData(recordsListDTO);
                            binding.recordsLin.addView(commitView);
                            break;
                        case 3://工单指派
                            FaultAssignView assignView = new FaultAssignView(FaultDetailActivity.this);
                            assignView.setData(recordsListDTO);
                            binding.recordsLin.addView(assignView);
                            break;
                        case 4:// 维修中
                            FaultRepairingView repairingView = new FaultRepairingView(FaultDetailActivity.this);
                            repairingView.setData(recordsListDTO);
                            binding.recordsLin.addView(repairingView);
                            break;
                        case 5:// 暂不维修
                            FaultRepairBackView repairBackView = new FaultRepairBackView(FaultDetailActivity.this);
                            repairBackView.setData(recordsListDTO);
                            binding.recordsLin.addView(repairBackView);
                            break;
                        case 6:// 未修复
                            FaultNotSuccessView notSuccessView = new FaultNotSuccessView(FaultDetailActivity.this);
                            notSuccessView.setData(recordsListDTO);
                            binding.recordsLin.addView(notSuccessView);
                            break;

                        case 7:// 已修复，待确认
                            FaultRepairGoneView repairGoneView = new FaultRepairGoneView(FaultDetailActivity.this);
                            repairGoneView.setData(recordsListDTO);
                            binding.recordsLin.addView(repairGoneView);
                            break;

                        case 8:// 已完成
                            FaultSuccessView successView = new FaultSuccessView(FaultDetailActivity.this);
                            successView.setData(recordsListDTO);
                            binding.recordsLin.addView(successView);
                            break;
                    }
                }
            }
        });

    }

    /**
     * 工单退回
     */
    private void showReBackPopop() {
        ReBackPopupView reBackPopupView = new ReBackPopupView(this);
        reBackPopupView.setCallBack(remark -> {
            viewModel.gdth(remark);
        });
        new XPopup.Builder(this)
                .asCustom(reBackPopupView)
                .show();
    }

    /**
     * 刷新
     * @param event
     */
    @Subscribe
    public void onEvent(FaultDetailEvent event){
        viewModel.refresh();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this);
        }
    }
}